function wordBreak(s, wordDict) {
    const wordSet = new Set(wordDict)
    const memo = {}

    function backtrack(s) {
       // 如果当前子字符串已经在缓存中，直接返回缓存的结果
       if (memo[s] !== undefined) {
          return memo[s]
       }
       // 如果子字符串为空，返回包含一个空字符串的数组
       if (s === '') {
          return ['']
       }

       const result = []

       // 遍历词典中的每个单词
       for (const word of wordSet) {
          if (s.startsWith(word)) {
             const subResult = backtrack(s.slice(word.length))
             for (const sub of subResult) {
                result.push(word + (sub === '' ? '' : ' ') + sub)
             }
          }
       }

       // 将当前子字符串的结果存入缓存
       memo[s] = result
       return result
    }

    return backtrack(s)
 }

 // 示例测试
 console.log(wordBreak('catsanddog', ['cat', 'cats', 'and', 'sand', 'dog']))
 // 输出：["cats and dog", "cat sand dog"]

 console.log(wordBreak('pineapplepenapple', ['apple', 'pen', 'applepen', 'pine', 'pineapple']))
 // 输出：["pine apple pen apple", "pineapple pen apple", "pine applepen apple"]

 console.log(wordBreak('catsandog', ['cats', 'dog', 'sand', 'and', 'cat']))
 // 输出：[]